package com.example.beiwang.tools;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;

import androidx.core.content.FileProvider;
import androidx.fragment.app.Fragment;

import java.io.File;
import java.io.IOException;

public class CameraUtil {
    public static File createImageFile(Context context){
        String imageFileName = String.valueOf(System.currentTimeMillis());
        File storageDir = context.getExternalCacheDir();
        File image = null;
        try {
            image = File.createTempFile(
                    imageFileName,  /* prefix */
                    ".jpg",   /* suffix */
                    storageDir      /* directory */
            );
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

    public static Uri OpenCamera(Fragment context, int requestCode){
        File image = createImageFile(context.getActivity());
        Uri imageUri = FileProvider.getUriForFile(context.getActivity(), "com.example.dontforget.fileprovider", image);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
        context.startActivityForResult(intent,requestCode);
        return imageUri;
    }

    public static void openAlbum(Fragment context,int requestCode){
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        context.startActivityForResult(intent,requestCode);
    }


}
